package com.leetroll.common.bean;

import lombok.Data;

import java.io.Serializable;

@Data
public class ApiResponse<T> implements Serializable {

    public static final int SYSTEM_FAIL_CODE = -1;
    public static final int SYSTEM_SUCCESS_CODE = 0;

    private int code = SYSTEM_SUCCESS_CODE;

    private String msg = "ok";

    private T data = null;

    public ApiResponse() {
    }

    public ApiResponse(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ApiResponse(int code, String msg, T data) {
        this(code, msg);
        if (data != null) {
            this.data = data;
        }
    }

    public static <T> ApiResponse<T> OK(T data) {
        return responseData(data);
    }

    public static <T> ApiResponse<T> OK() {
        return new ApiResponse<>();
    }

    public static <T> ApiResponse<T> FAIL() {
        return new ApiResponse<>(SYSTEM_FAIL_CODE, "fail");
    }

    public static <T> ApiResponse<T> FAIL(String msg) {
        return new ApiResponse<>(SYSTEM_FAIL_CODE, msg);
    }

    public static <T> ApiResponse<T> FAIL(int code, String msg) {
        return new ApiResponse<>(code, msg);
    }

    private static <T> ApiResponse<T> responseData(T data) {
        ApiResponse<T> response = new ApiResponse<>();
        response.setData(data);
        return response;
    }

}
